package top.ewind.interceptor;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.json.FastJson;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;

import top.ewind.common.AppConfig;
import top.ewind.model.SysDictory;
import top.ewind.util.SecurityUtil;

public class WxInterceptor  implements Interceptor {
	public void intercept(Invocation ai) {
		/*Controller controller = ai.getController();
		int nid = 0;
		try {
			String idstr = SecurityUtil.decryptByDES("vpower", controller.getPara(0,"0"));
			nid = Integer.parseInt(idstr);
		} catch (Exception e) {}
		Record cuser = CacheKit.get("cuser",nid);
		if(cuser==null){
			cuser = Db.findById("cuser", nid);
			CacheKit.put("cuser", nid, cuser);
		}
		//controller.setSessionAttr("openid-"+nid,"123456");  //("openid-"+nid)
		//controller.setSessionAttr("nickname","张三哥");
		if(cuser!=null){
			if(controller.getSessionAttr("openid-"+nid)!=null){
				String openid = controller.getSessionAttr("openid-"+nid);
				String usertoken = controller.getCookie("usertoken");
				String userid = controller.getSessionAttr("userid");
				Integer nownid = controller.getSessionAttr("nownid");
				if(StrKit.isBlank(usertoken)||StrKit.isBlank(userid)||nownid==null||nownid.intValue()!=nid){
					String rep = IotHttp.userLogin(nid,openid);
					HashMap<?,?> jsonmap = new FastJson().parse(rep, HashMap.class);
					controller.setSessionAttr("userid",(String)jsonmap.get("id"));
					controller.setSessionAttr("nownid",nid);
					controller.setSessionAttr("userkey",(String)jsonmap.get("userkey"));
					controller.setCookie("usertoken", (String)jsonmap.get("usertoken"), (Integer)jsonmap.get("expire"));
				}				
				controller.setAttr("nid",controller.getPara(0));
				controller.setAttr("dnid",nid);
				ai.invoke();
			}
			else{
				String ua = controller.getRequest().getHeader("user-agent").toLowerCase();
				if(cuser.getInt("acounttype").intValue()==3&&ua.indexOf("micromessenger") > 0&&StrKit.notBlank(cuser.getStr("appid"))&&StrKit.notBlank(cuser.getStr("appsecret"))){
					String reurl = SysDictory.getConfig("mqttserver")+"/wap/authorcode?dn="+controller.getPara(0)+"&succ=fans";
					controller.setCookie("FANS_OP_REURL", getHttpUrl(controller.getRequest()), -1);
					try {
						reurl = URLEncoder.encode(reurl, "utf-8");
						String authorurl = WeixinUtil.code_url.replace("SCOPE", "snsapi_userinfo").replace("STATE", "fans")
								.replace("APPID",cuser.getStr("appid")).replace("REDIRECT_URI",reurl);
						controller.redirect(authorurl);
						return;
					} catch (UnsupportedEncodingException e) {controller.renderText("系统编码发生错误");}		
				}
				else{
					controller.renderText("公众帐号类型有误，只限于已认证的服务号");
				}
			}
		}
		else{
			controller.renderText("访问链接有误");
		}
	}
	
	private String getHttpUrl(HttpServletRequest request){
		String strUrl = "http://" + request.getServerName() + request.getContextPath() + request.getServletPath();
    	if(StrKit.notBlank(request.getQueryString())){
    		strUrl += "?"+request.getQueryString();
    	}
		return strUrl;*/
	}
}
